﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CCWin;

namespace weiqijiance
{
    public partial class jdsjcx : CCSkinMain
    {
        public jdsjcx()
        {
            InitializeComponent();
        }
        public mainform m_mainform;
        public string time;
        public string linecode;
        private void button1_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();
            string ssql = string.Format(@"select * from JDSJ where JDSJ_MC like '%{0}%' and JDSJ_RQ>='{1}' and JDSJ_RQ<'{2}' order by JDSJ_ID", comboBox1.Text.Trim(), dateTimePicker1.Value.ToString("yyyy-MM-dd 00:00:01"), dateTimePicker2.Value.ToString("yyyy-MM-dd 23:59:59"));
            DataTable dt = m_mainform.objbass.objdb.GetTable(ssql);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ListViewItem li = new ListViewItem();
                li.Text = (i + 1).ToString();
                li.SubItems.Add(dt.Rows[i]["JDSJ_MC"].ToString().Trim());
                li.SubItems.Add(dt.Rows[i]["JDSJ_JG"].ToString().Trim());
                li.SubItems.Add(dt.Rows[i]["JDSJ_RQ"].ToString().Trim());
                li.SubItems.Add(dt.Rows[i]["JDSJ_JCX"].ToString().Trim());
                listView1.Items.Add(li);
            }
        }

        private void jdsjcx_Load(object sender, EventArgs e)
        {
            this.Resize += new EventHandler(Form1_Resize);

            X = this.Width;
            Y = this.Height;


            setTag(this);
            Form1_Resize(new object(), new EventArgs());//x,y可在实例化时赋值,最后这句是新加的，在MDI时有用

            comboBox1.Items.Clear();
            string ssql = string.Format(@"select distinct JDSJ_MC from JDSJ");
            DataTable dt = m_mainform.objbass.objdb.GetTable(ssql);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if(string.IsNullOrWhiteSpace(dt.Rows[i]["JDSJ_MC"].ToString().Trim()))
                    continue;
                comboBox1.Items.Add(dt.Rows[i]["JDSJ_MC"].ToString().Trim());
            }
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (ListViewItem li in listView1.SelectedItems)
            {
                richTextBox1.Text = li.SubItems[2].Text.Trim();
                time = li.SubItems[3].Text.Trim();
                linecode = li.SubItems[4].Text.Trim();
            }
        }


        private float X;

        private float Y;

        private void setTag(Control cons)
        {
            foreach (Control con in cons.Controls)
            {
                con.Tag = con.Width + ":" + con.Height + ":" + con.Left + ":" + con.Top + ":" + con.Font.Size;
                if (con.Controls.Count > 0)
                    setTag(con);
            }
        }
        private void setControls(float newx, float newy, Control cons)
        {
            foreach (Control con in cons.Controls)
            {

                string[] mytag = con.Tag.ToString().Split(new char[] { ':' });
                float a = Convert.ToSingle(mytag[0]) * newx;
                con.Width = (int)a;
                a = Convert.ToSingle(mytag[1]) * newy;
                con.Height = (int)(a);
                a = Convert.ToSingle(mytag[2]) * newx;
                con.Left = (int)(a);
                a = Convert.ToSingle(mytag[3]) * newy;
                con.Top = (int)(a);
                Single currentSize = Convert.ToSingle(mytag[4]) * Math.Min(newx, newy);
                if (currentSize <= 0)
                {
                    currentSize = 0.1f;
                }
                con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
                if (con.Controls.Count > 0)
                {
                    setControls(newx, newy, con);
                }
            }

        }

        void Form1_Resize(object sender, EventArgs e)
        {
            float newx = (this.Width) / X;
            float newy = this.Height / Y;
            setControls(newx, newy, this);
           // this.Text = this.Width.ToString() + " " + this.Height.ToString();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            string sql = string.Format(@"delete JDSJ where JDSJ_RQ ='{0}'and JDSJ_JCX='{1}'", time, linecode);
            m_mainform.objbass.objdb.GetTable(sql);
            button1_Click(sender, e);
        }

        private void jdsjcx_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F6)
            {
                button2.Visible = true;
            }
        }

    }
}
